package com.mazaiting.redeye.module.main

import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.annotation.NonNull
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.mazaiting.redeye.R
import com.mazaiting.redeye.module.friend.FriendFragment
import com.mazaiting.redeye.module.me.MeFragment
import com.mazaiting.redeye.module.media.MediaFragment
import com.mazaiting.redeye.module.msg.MsgFragment


/**
 * 主页面
 */
class MainActivity : AppCompatActivity() {

    /**
     * 底部导航
     */
    private lateinit var mNavigation:BottomNavigationView
    /**
     * 轮播控件
     */
    private lateinit var mViewPager: ViewPager
    /**
     * Fragment 列表
     */
    private val mFragments = mutableListOf<Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initFragments()

        // 获取 ViewPager
        mViewPager = findViewById(R.id.view_pager)
        // 获取底部导航
        mNavigation = findViewById(R.id.bottom_navigation)
        // 标题
        val titles = arrayOf(getString(R.string.menu_msg),getString(R.string.menu_review), getString(R.string.menu_work), getString(R.string.menu_me))
        // 设置缓存页面数, 缓存 3 页 + 正在显示的 1 页
        mViewPager.offscreenPageLimit = 3
        // 设置页面改变监听
        mViewPager.addOnPageChangeListener(OnCustomPageChangeListener())
        // 设置适配器
        mViewPager.adapter = MainViewPagerAdapter(supportFragmentManager, mFragments, titles)
        // 设置条目选中监听
        mNavigation.setOnNavigationItemSelectedListener(OnCustomNavigationItemSelectedListener())
    }

    /**
     * 初始化工作碎片
     */
    private fun initFragments() {
        // 添加消息页面
        mFragments.add(MsgFragment.newInstance())
        // 添加我的碎片
        mFragments.add(FriendFragment.newInstance())
        // 添加媒体碎片
        mFragments.add(MediaFragment.newInstance())
        // 添加我的碎片
        mFragments.add(MeFragment.newInstance())
    }

    override fun onBackPressed() {
        quitApplication()
    }

    /**
     * 退出应用, 点击返回键不退出应用
     */
    private fun quitApplication() {
        val home = Intent(Intent.ACTION_MAIN)
        home.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
        home.addCategory(Intent.CATEGORY_HOME)
        startActivity(home)
    }

    /**
     * 自定义 ViewPager 页面改变监听
     */
    private inner class OnCustomPageChangeListener : ViewPager.OnPageChangeListener {
        override fun onPageScrolled(i: Int, v: Float, i1: Int) {}

        override fun onPageSelected(position: Int) {
            when (position) {
                0 -> mNavigation.selectedItemId = R.id.navigation_msg
                1 -> mNavigation.selectedItemId = R.id.navigation_review
                2 -> mNavigation.selectedItemId = R.id.navigation_work
                3 -> mNavigation.selectedItemId = R.id.navigation_me
            }
        }

        override fun onPageScrollStateChanged(i: Int) {

        }
    }

    /**
     * 自定义条目选择监听
     */
    private inner class OnCustomNavigationItemSelectedListener : BottomNavigationView.OnNavigationItemSelectedListener {

        override fun onNavigationItemSelected(@NonNull menuItem: MenuItem): Boolean {
            when (menuItem.itemId) {
                R.id.navigation_msg -> {
                    mViewPager.currentItem = 0
                    return true
                }
                R.id.navigation_review -> {
                    mViewPager.currentItem = 1
                    return true
                }
                R.id.navigation_work -> {
                    mViewPager.currentItem = 2
                    return true
                }
                R.id.navigation_me -> {
                    mViewPager.currentItem = 3
                    return true
                }
            }
            return false
        }
    }
}
